package com.niuwa.p2p.action.customer;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.common.ICommonService;
import com.niuwa.p2p.service.customer.ICustomerService;
import com.niuwa.p2p.vo.customer.AttachmentVo;
import com.niuwa.p2p.vo.customer.CustomerAuditVo;

public class CustomerAuditAction extends JsonAction {
	
	/** 用户Servcie spring扫包注入 */
	@Autowired
	private ICustomerService customerService;
	
	@Autowired
	private ICommonService commonService;
	
	private AttachmentVo attachmentVo;
	
	
	private String queryParamKey;
	
	private String queryParamValue;
	
	/** 客户ID */
	private long custId;
	
	/** ID */
	private long id;
	
	/** 申请账户 */
	private String auditType;
	
	/** 申请类型 */
	private String auditAction;
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getAuditAction() {
		return auditAction;
	}

	public void setAuditAction(String auditAction) {
		this.auditAction = auditAction;
	}

	public long getCustId() {
		return custId;
	}

	public void setCustId(long custId) {
		this.custId = custId;
	}

	public String getAuditType() {
		return auditType;
	}

	public void setAuditType(String auditType) {
		this.auditType = auditType;
	}

	public String getQueryParamKey() {
		return queryParamKey;
	}

	public void setQueryParamKey(String queryParamKey) {
		this.queryParamKey = queryParamKey;
	}

	public String getQueryParamValue() {
		return queryParamValue;
	}

	public void setQueryParamValue(String queryParamValue) {
		this.queryParamValue = queryParamValue;
	}

	public AttachmentVo getAttachmentVo() {
		return attachmentVo;
	}

	public void setAttachmentVo(AttachmentVo attachmentVo) {
		this.attachmentVo = attachmentVo;
	}
	
	@Action("gotoAuditList")
	public String gotoAuditList() {
		return "cancelAuditList";
	}

	/**
	 * 审核列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "auditList")
	public String auditList() throws Exception {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		if(paramMap.get("queryParamKey") != null && paramMap.get("queryParamKey") instanceof String){	
			queryParamKey = (String) paramMap.get("queryParamKey");
			queryParamValue = (String) paramMap.get("queryParamValue");
			paramMap.put((String) paramMap.get("queryParamKey"), paramMap.get("queryParamValue"));
		}
		PageBean<CustomerAuditVo> pagination = customerService.selectCustomerAuditList(paramMap);
		setObjectReSult(pagination);
		return NONE;
	}
	
	/**
	 * 通过审核
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "passAudit")
	public String passAuditInfo(){
		JsonResult result = RESULT_SUCCESS;
		try {
			CustomerAuditVo auditVo = new CustomerAuditVo();
			auditVo.setCustId(custId);
			auditVo.setAuditType(auditType);
			auditVo.setAuditAction(auditAction);
			auditVo.setAuditor(String.valueOf(SessionUtil.getSessionUser().getId()));
			int num = customerService.updatePlatOrAccountStatusOff(auditVo);
			if(num > 0){
				//审核成功
				this.setDefaultSuccessResult();
			}else{
				//更新失败
				this.setDefaultFailedResult();
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(e.getMessage());
			setResult(result);
			return NONE;
		}
		return NONE;
	}
	
	/**
	 * 审核拒绝
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "refusedAudit")
	public String refusedAuditInfo(){
		JsonResult result = RESULT_SUCCESS;
		try {
			CustomerAuditVo auditVo = new CustomerAuditVo();
			auditVo.setCustId(custId);
			auditVo.setAuditType(auditType);
			auditVo.setAuditor(String.valueOf(SessionUtil.getSessionUser().getId()));
			int num = customerService.updateRefusedAuditInfo(auditVo);
			if(num > 0){
				//审核成功
				this.setDefaultSuccessResult();
			}else{
				//更新失败
				this.setDefaultFailedResult();
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(e.getMessage());
			return NONE;
		}
		return NONE;
	}
	
	/**
	 * 附件下载详情页面
	 * @return
	 */
	@Action("gotoCancelDownload")
	public String gotoCancelDownload() {
		//用户ID
		if (id != 0 && !"".equals(id)) {
			try {
				attachmentVo = commonService.getAttachmentBySelective(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "cancelDownload";
	}

	
}
